package cn.edu.njust.strategy;

import cn.edu.njust.server.Stat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class WeightedRandomStrategy implements StatefulStrategy {
    private Random random = new Random();

    private List<Integer> repetedIndices;

    @Override
    public int next(List<Stat> stats, List<Integer> weights) {
        return repetedIndices.get(random.nextInt(repetedIndices.size()));
    }

    @Override
    public void init(List<Stat> stats, List<Integer> weights) {
        repetedIndices = new ArrayList<>();
        for (int i = 0; i < weights.size(); i++) {
            int weight = weights.get(i);
            while (weight-- > 0)
                repetedIndices.add(i);
        }
        Collections.shuffle(repetedIndices);
    }

    @Override
    public void reorg(List<Stat> stats, List<Integer> weights) {
        init(stats, weights);
    }
}
